package XiaoeApiSDK

import (
	"encoding/json"
	"fmt"
	"net/http"
	"os"
	"server/global"
	"server/internal/pkg/XiaoeApiSDK/demo"
	"strings"

	"github.com/faabiosr/cachego/file"
)

func Client() *demo.Client {

	currentDir, _ := os.Getwd()
	xiaoE := &demo.DefaultAccessTokenManager{
		Id:   global.GL_CONFIG.XiaoEr.XiaoEAppId,
		Name: "access_token",
		GetRefreshRequestFunc: func() *http.Request {
			params := make(map[string]string)
			params["app_id"] = global.GL_CONFIG.XiaoEr.XiaoEAppId
			params["client_id"] = global.GL_CONFIG.XiaoEr.XiaoEClientId
			params["secret_key"] = global.GL_CONFIG.XiaoEr.XiaoEAppSecret
			params["grant_type"] = global.GL_CONFIG.XiaoEr.XiaoEGrantType

			str, err := json.Marshal(params)
			if err != nil {
				fmt.Println(err)
			}
			payload := strings.NewReader(string(str))
			req, err := http.NewRequest(http.MethodGet, demo.ServerUrl, payload)
			if err != nil {
				return nil
			}

			return req
		},
		// os.TempDir() - 设置缓存路径，默认为系统默认缓存路径,为了方便查找建议修改路径
		Cache: file.New(currentDir),
	}

	// 小鹅云 客户端
	return demo.NewClient(xiaoE)

}
